package org.aos.util;

import java.io.*;
import org.apache.log4j.Logger;
import org.apache.struts.upload.FormFile;

public class UploadFileOne
{
	private static Logger log = Logger.getLogger(UploadFileOne.class);
    private static int fileSize = 2048;
    public static String strPath = "images/advertisement/";
    public static String ZY_path="zydir/";

    public UploadFileOne()
    {
    }

    public static String getFileType(FormFile formFile)
    {
        String filebackstr = "";
        String contentType = formFile.getContentType();
        if(contentType.equals("image/pjpeg"))
            filebackstr = ".jpg";
        else
        if(contentType.equals("image/gif"))
            filebackstr = ".gif";
        else
        if(contentType.equals("image/bmp"))
            filebackstr = ".bmp";
        return filebackstr;
    }

    public static String fileUploadOne(FormFile formFile, String rootFilePath, String strAppend)
    {
        String strReturn = null;
        if(formFile == null)
            return strReturn;
        log.info("\u4E0A\u4F20\u7684\u6587\u4EF6\u4E0D\u4E3A\u7A7A!");
        try
        {
            InputStream stream = formFile.getInputStream();
            
            if(formFile.getFileSize() > fileSize * 1024*10)
            {
                strReturn = "\u8bf7\u628a\u5bfc\u5165\u6587\u4ef6\u63a7\u5236\u572820M\u4ee5\u5185\uff01";
                return strReturn;
            }
            String contentType = formFile.getContentType();
            String filebackstr = "";
            if(contentType.equals("image/pjpeg"))
                filebackstr = ".jpg";
            else
            if(contentType.equals("image/gif"))
                filebackstr = ".gif";
            else
            if(contentType.equals("image/bmp"))
            {
                filebackstr = ".bmp";
            } 
//            else
//            {
//                strReturn = "\u6587\u4EF6\u683C\u5F0F\u4E0D\u6B63\u786E\uFF01";
//                return strReturn;
//            }
            log.info((new StringBuilder("\u6587\u4EF6\u7C7B\u578B\u4E3A\uFF1A\uFF1A")).append(filebackstr).toString());
            File rootDir = new File(rootFilePath);
            if(!rootDir.exists() && rootDir.mkdirs())
                log.info((new StringBuilder("\u521B\u5EFA\u4E0A\u4F20\u6587\u4EF6\u6839\u76EE\u5F55\u6210\u529F\uFF1A")).append(rootFilePath).toString());
            OutputStream bos = null;
            //File checkFile = new File((new StringBuilder(String.valueOf(rootFilePath))).append(strAppend).toString());
            /*if(checkFile.exists())
            {
                strReturn = "\u6587\u4EF6\u5DF2\u7ECF\u5B58\u5728";
                return strReturn;
            }*/
            bos = new FileOutputStream((new StringBuilder(String.valueOf(rootFilePath))).append(strAppend).toString());
            int bytesRead = 0;
            byte buffer[] = new byte[8192];
            while((bytesRead = stream.read(buffer, 0, 8192)) != -1) 
                bos.write(buffer, 0, bytesRead);
            bos.close();
            stream.close();
        }
        catch(Exception e)
        {
            strReturn = "\u4E0A\u4F20\u6587\u4EF6\u51FA\u9519!";
            e.printStackTrace();
            return strReturn;
        }
        return strReturn;
    }
}